package com.typhoon.spring_redis.dao;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.typhoon.spring_redis.entity.User;

@Repository
public class UserDao {

	private static final String TABLE_USER = " User ";

	@Resource
	private JdbcTemplate jdbcTemplate;

	public User queryByPK(Long id) {
		StringBuilder sql = new StringBuilder();
		sql.append(" select ");
		sql.append(" * ");
		sql.append("  from  ");
		sql.append(TABLE_USER);
		sql.append("  where  ");
		sql.append("  id = ?  ");
		sql.append(" limit 1 ");
		
		System.out.println(sql.toString());

		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql.toString(), new Object[] { id });

		if (null == list || list.isEmpty()) {
			return null;
		}
		User u = new User();
		Map<String, Object> map = list.iterator().next();
		try {
			BeanUtils.populate(u, map);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return u;
	}

}
